package com.ch_linghu.fanfoudroid.db;

import com.ch_linghu.fanfoudroid.data.Bus;
 

import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

public class BusTable implements BaseColumns {

	public static final String TAG = "BusTable";

	public static final String TABLE_NAME = "bus";
	
	public static final String FIELD_NAME = "name";
	public static final String FIELD_CITY  = "city";
	public static final String FIELD_LOCALTION = "location";
	public static final String FIELD_PATH = "path";
	public static final String FIELD_INTRO = "intro"; 
	public static final String FIELD_ANNOUNCE = "announce"; 
	public static final String FIELD_MEMBER_COUNT = "membercount";
	
	public static final String[] TABLE_COLUMNS = new String[] { _ID,
		FIELD_NAME, FIELD_CITY,
		FIELD_LOCALTION, FIELD_PATH,
		FIELD_INTRO, FIELD_ANNOUNCE, FIELD_MEMBER_COUNT
	};
	
	public static final String CREATE_TABLE = "create table " + TABLE_NAME
	+ " (" + _ID + " text primary key on conflict replace, "
	+ FIELD_NAME + " text not null, " + FIELD_CITY + " text, " + FIELD_LOCALTION
	+ " text, " + FIELD_PATH + " text, " + FIELD_INTRO
	+ " text, " + FIELD_ANNOUNCE + " text, "
	+ FIELD_MEMBER_COUNT + " integer"  
	+ ")";
	
	/**
	 * TODO: 将游标解析为一条bus信息
	 * 
	 * @param cursor
	 *            该方法不会关闭游标
	 * @return 成功返回Bus类型的单条数据, 失败返回null
	 */
	public static Bus parseCursor(Cursor cursor) {
		
		if (null == cursor || 0 == cursor.getCount()) {
			Log.w(TAG, "Cann't parse Cursor, bacause cursor is null or empty.");
			return null;
		}

		Bus bus = new Bus();
		bus.bid = cursor.getString(cursor.getColumnIndex(_ID));
		bus.name = cursor.getString(cursor.getColumnIndex(FIELD_NAME));
		bus.city = cursor.getString(cursor.getColumnIndex(FIELD_CITY));
		bus.location =cursor.getString(cursor.getColumnIndex(FIELD_LOCALTION));
		bus.path =cursor.getString(cursor.getColumnIndex(FIELD_PATH));
		bus.intro =cursor.getString(cursor.getColumnIndex(FIELD_INTRO));
		bus.announce =cursor.getString(cursor.getColumnIndex(FIELD_ANNOUNCE));
		bus.memberCount = cursor.getInt(cursor.getColumnIndex(FIELD_MEMBER_COUNT));
		
		return bus;
		
	}
}
